import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;

import org.jdesktop.swingx.JXDatePicker;

import CustomSwing.JCLabel;
import CustomSwing.TableColumnAdjuster;

@SuppressWarnings("serial")
public class OutstandingTransPanel extends JPanel {
	public String USER_FUNC = "Outstanding Transactions";
	private Font custFont = new Font("Arial Bold", Font.BOLD, 15);
	
	private Db db;
	private String dateStart;
	private String dateEnd;
	private JXDatePicker datePickerStart = new JXDatePicker();
	private JXDatePicker datePickerEnd = new JXDatePicker();
	
	private String[] columnNamesType = {"Date", "Business Name", "Vendor ID"};
	private String[][] data;
	private JTable table;
	private JScrollPane tableSP;
	private JPanel tableSPPanel = new JPanel(new BorderLayout());
	
	public OutstandingTransPanel(Db theDB) {
		db = theDB;
		setDatePicker(datePickerStart, true);
		setDatePicker(datePickerEnd, false);
		
		Border b = BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK), 
				BorderFactory.createEmptyBorder(3, 3, 3, 3));
		
		JPanel startDatePanel = new JPanel(new GridLayout(2, 1));
		startDatePanel.setBorder(b);
		startDatePanel.add(new JCLabel("Start Date"));
		startDatePanel.add(datePickerStart);
		
		JPanel endDatePanel = new JPanel(new GridLayout(2, 1));
		endDatePanel.setBorder(b);
		endDatePanel.add(new JCLabel("End Date"));
		endDatePanel.add(datePickerEnd);

		JPanel optionsPanel = new JPanel(new GridLayout(1, 3, 5, 0));
		optionsPanel.add(startDatePanel);
		optionsPanel.add(endDatePanel);
		optionsPanel.add(new JPanel());

		setLayout(new BorderLayout(5, 5));
		add(optionsPanel, BorderLayout.NORTH);
		add(tableSPPanel, BorderLayout.CENTER);
		
		refreshTablePanel();
	}
	
	public void refreshTablePanel() {
		tableSPPanel.removeAll();
		getData();
		tableSPPanel.add(tableSP);
		tableSPPanel.revalidate();
	}
	
	private void getData() {
		try {
			ArrayList<String[]> totals = db.outstandingTransactions(dateStart, dateEnd);
			data = new String[totals.size()][columnNamesType.length];
			for (int i = 0; i < totals.size(); i++) {
				String[] totalsData = totals.get(i);
				data[i][0] = totalsData[0].substring(5, 7) + "-" 
						+ totalsData[0].substring(8, 10) + "-" 
						+ totalsData[0].substring(0, 4);
				for (int j = 1; j < totalsData.length; j++) {
					if (totalsData[j] == null)data[i][j] = "";
					else data[i][j] = " "+totalsData[j]+" ";
				}
			}
			setTable(columnNamesType);
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void setTable(String[] columnNames) {
		table = new JTable(data, columnNames);
		table.setFont(new Font(custFont.getName(), Font.PLAIN, custFont.getSize()-2));
		table.setEnabled(false);
		table.setRowHeight(20);
		table.getTableHeader().setFont(
				new Font(custFont.getName(), Font.PLAIN, custFont.getSize()-1));
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumnAdjuster tca = new TableColumnAdjuster(table);
		tca.adjustColumns();
		tableSP = new JScrollPane(table);
		tableSP.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK), 
				BorderFactory.createEmptyBorder(3, 3, 3, 3)));
	}
	
	private void setDatePicker(final JXDatePicker dp, final boolean startingDate) {
		if (startingDate) {
			try {
				String s = db.getEarliestDate();
				int year = Integer.parseInt(s.substring(0, 4));
				int month = Integer.parseInt(s.substring(5, 7)) - 1;
				int day = Integer.parseInt(s.substring(8, 10));
				dp.setDate(new GregorianCalendar(year, month, day).getTime());
				
			} catch (ClassNotFoundException e) {
			} catch (SQLException e) {}
		} else {
			dp.setDate(Calendar.getInstance().getTime());
		}
		dp.setFormats(new SimpleDateFormat("MM / dd / yyyy"));
		dp.getEditor().setEditable(false);
		dp.getEditor().setFont(custFont);
		dp.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				String s = dp.getEditor().getText().trim();
				String date = s.substring(10, 14) + "-" + s.substring(0, 2) + "-" + s.substring(5, 7);
				if (startingDate) dateStart = date;
				else dateEnd = date;				
				if (datePickerStart.getDate().after(datePickerEnd.getDate())) {
					JOptionPane.showConfirmDialog(null, "Start date is after the end date!", 
							"Warning", JOptionPane.CLOSED_OPTION); 
				}
				refreshTablePanel();
			}
		});
		String s = dp.getEditor().getText().trim();
		String date = s.substring(10, 14) + "-" + s.substring(0, 2) + "-" + s.substring(5, 7);
		if (startingDate) dateStart = date; else dateEnd = date;
	}
}